package com.shwread.android.qysw.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.GlideUtil;
import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.bean.QRCodeBean;
import com.shwread.android.qysw.bean.QRCodeBean.StaffBean;
import com.shwread.android.qysw10000227.R;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 我的二维码界面
 */
public class QRCodeActivity extends BaseSimpleActivity implements OnClickListener {
    private Button btnBack;
    private TextView tvTitle;
    private TextView tv_my_subordinate_title;
    private RecyclerView rlv_subordinate_list;
    private ImageView img_qr_code;
    /**
     * 下线列表
     */
    private List<StaffBean> staffBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qr_code_activity);
        initView();
        setListener();
        NewActionHelp.getUserQR(this, new ObjectHttpResponseHandler<QRCodeBean>() {

            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("11111","error--" + s);
            }

            @Override
            public void onSuccess(int i, Header[] headers, QRCodeBean qrCodeBean, String s) {
                Log.e("11111","ok--" + s);
                GlideUtil.load(QRCodeActivity.this, qrCodeBean.getUrl(), img_qr_code);
                staffBeanList = qrCodeBean.getStaff();
                setList();
            }

            @Override
            public Type getType() {
                return new TypeToken<QRCodeBean>(){
                }.getType();
            }
        });
    }

    private void initView() {
        btnBack = (Button) findViewById(R.id.common_header_back);
        tvTitle = (TextView) findViewById(R.id.common_header_name);
        tv_my_subordinate_title = (TextView) findViewById(R.id.tv_my_subordinate_title);
        img_qr_code = (ImageView) findViewById(R.id.img_qr_code);
        rlv_subordinate_list = (RecyclerView) findViewById(R.id.rlv_subordinate_list);
        btnBack.setVisibility(View.VISIBLE);
        tvTitle.setText("我的二维码");
    }

    private void setListener() {
        btnBack.setOnClickListener(this);
//        tv_my_subordinate_title.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back://返回
                finish();
                break;
            case R.id.tv_my_subordinate_title://我的下属
                if (rlv_subordinate_list.getVisibility() == View.GONE) {
                    rlv_subordinate_list.setVisibility(View.VISIBLE);
                } else {
                    rlv_subordinate_list.setVisibility(View.GONE);
                }
                break;
        }
    }

    private void setList() {
        rlv_subordinate_list.setLayoutManager(new LinearLayoutManager(this));
        rlv_subordinate_list.setAdapter(new MyAdapter());
    }

    private class MyAdapter extends RecyclerView.Adapter<ViewHolder> {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(QRCodeActivity.this).inflate(R.layout.subordinate_list_item,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            StaffBean staffBean = staffBeanList.get(position);
            holder.subordinateName.setText(staffBean.getStaffName());
            holder.subordinatePhone.setText(staffBean.getAccount());
        }

        @Override
        public int getItemCount() {
            return staffBeanList.size();
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView subordinateName;
        private TextView subordinatePhone;

        public ViewHolder(View itemView) {
            super(itemView);
            subordinateName = (TextView) itemView.findViewById(R.id.tv_subordinate_name);
            subordinatePhone = (TextView) itemView.findViewById(R.id.tv_subordinate_phone);
        }
    }
}
